home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Comm / www / tidy_os4.lha / tidy / src / buffio.c < prev    next >
C/C++ Source or Header  |  2004-07-25  |  4KB  |  193 lines

  1. /* buffio.c -- Treat buffer as an I/O stream.
  2.  
  3.   (c) 1998-2004 (W3C) MIT, ERCIM, Keio University
  4.   See tidy.h for the copyright notice.
  5.  
  6.   CVS Info :
  7.  
  8.     $Author: terry_teague $ 
  9.     $Date: 2004/02/29 03:58:12 $ 
  10.     $Revision: 1.7 $ 
  11.  
  12.   Requires buffer to automatically grow as bytes are added.
  13.   Must keep track of current read and write points.
  14.  
  15. */
  16.  
  17. #include "tidy.h"
  18. #include "buffio.h"
  19.  
  20.  
  21. /**************
  22.    TIDY
  23. **************/
  24.  
  25. static int insrc_getByte( ulong appData )
  26. {
  27.   TidyBuffer* buf = (TidyBuffer*) appData;
  28.   return tidyBufGetByte( buf );
  29. }
  30. static Bool insrc_eof( ulong appData )
  31. {
  32.   TidyBuffer* buf = (TidyBuffer*) appData;
  33.   return tidyBufEndOfInput( buf );
  34. }
  35. static void insrc_ungetByte( ulong appData, byte bv )
  36. {
  37.   TidyBuffer* buf = (TidyBuffer*) appData;
  38.   tidyBufUngetByte( buf, bv );
  39. }
  40.  
  41. void  initInputBuffer( TidyInputSource* inp, TidyBuffer* buf )
  42. {
  43.   inp->getByte    = insrc_getByte;
  44.   inp->eof        = insrc_eof;
  45.   inp->ungetByte  = insrc_ungetByte;
  46.   inp->sourceData = (ulong) buf;
  47. }
  48.  
  49. static void outsink_putByte( ulong appData, byte bv )
  50. {
  51.   TidyBuffer* buf = (TidyBuffer*) appData;
  52.   tidyBufPutByte( buf, bv );
  53. }
  54.  
  55. void  initOutputBuffer( TidyOutputSink* outp, TidyBuffer* buf )
  56. {
  57.   outp->putByte  = outsink_putByte;
  58.   outp->sinkData = (ulong) buf;
  59. }
  60.  
  61.  
  62. void      tidyBufInit( TidyBuffer* buf )
  63. {
  64.     assert( buf != NULL );
  65.     ClearMemory( buf, sizeof(TidyBuffer) );
  66. }
  67.  
  68. void      tidyBufAlloc( TidyBuffer* buf, uint allocSize )
  69. {
  70.     tidyBufInit( buf );
  71.     tidyBufCheckAlloc( buf, allocSize, 0 );
  72.     buf->next = 0;
  73. }
  74. void      tidyBufFree( TidyBuffer* buf )
  75. {
  76.     assert( buf != NULL );
  77.     MemFree( buf->bp );
  78.     tidyBufInit( buf );
  79. }
  80.  
  81. void      tidyBufClear( TidyBuffer* buf )
  82. {
  83.     assert( buf != NULL );
  84.     if ( buf->bp )
  85.     {
  86.         ClearMemory( buf->bp, buf->allocated );
  87.         buf->size = 0;
  88.     }
  89.     buf->next = 0;
  90. }
  91.  
  92. /* Avoid thrashing memory by doubling buffer size
  93. ** until larger than requested size.
  94. */
  95. void tidyBufCheckAlloc( TidyBuffer* buf, uint allocSize, uint chunkSize )
  96. {
  97.     assert( buf != NULL );
  98.     if ( 0 == chunkSize )
  99.         chunkSize = 256;
  100.     if ( allocSize > buf->allocated )
  101.     {
  102.         byte* bp;
  103.         uint allocAmt = chunkSize;
  104.         if ( buf->allocated > 0 )
  105.             allocAmt = buf->allocated;
  106.         while ( allocAmt < allocSize )
  107.             allocAmt *= 2;
  108.  
  109.         bp = (byte*)MemRealloc( buf->bp, allocAmt );
  110.         if ( bp != NULL )
  111.         {
  112.             ClearMemory( bp + buf->allocated, allocAmt - buf->allocated );
  113.             buf->bp = bp;
  114.             buf->allocated = allocAmt;
  115.         }
  116.     }
  117. }
  118.  
  119. /* Attach buffer to a chunk O' memory w/out allocation */
  120. void      tidyBufAttach( TidyBuffer* buf, byte* bp, uint size )
  121. {
  122.     assert( buf != NULL );
  123.     buf->bp = bp;
  124.     buf->size = buf->allocated = size;
  125.     buf->next = 0;
  126. }
  127.  
  128. /* Clear pointer to memory w/out deallocation */
  129. void      tidyBufDetach( TidyBuffer* buf )
  130. {
  131.     tidyBufInit( buf );
  132. }
  133.  
  134.  
  135. /**************
  136.    OUTPUT
  137. **************/
  138.  
  139. void      tidyBufAppend( TidyBuffer* buf, void* vp, uint size )
  140. {
  141.     assert( buf != NULL );
  142.     if ( vp != NULL && size > 0 )
  143.     {
  144.         tidyBufCheckAlloc( buf, buf->size + size, 0 );
  145.         memcpy( buf->bp + buf->size, vp, size );
  146.         buf->size += size;
  147.     }
  148. }
  149.  
  150. void      tidyBufPutByte( TidyBuffer* buf, byte bv )
  151. {
  152.     assert( buf != NULL );
  153.     tidyBufCheckAlloc( buf, buf->size + 1, 0 );
  154.     buf->bp[ buf->size++ ] = bv;
  155. }
  156.  
  157.  
  158. int      tidyBufPopByte( TidyBuffer* buf )
  159. {
  160.     int bv = EOF;
  161.     assert( buf != NULL );
  162.     if ( buf->size > 0 )
  163.       bv = buf->bp[ --buf->size ];
  164.     return bv;
  165. }
  166.  
  167. /**************
  168.    INPUT
  169. **************/
  170.  
  171. int       tidyBufGetByte( TidyBuffer* buf )
  172. {
  173.     int bv = EOF;
  174.     if ( ! tidyBufEndOfInput(buf) )
  175.       bv = buf->bp[ buf->next++ ];
  176.     return bv;
  177. }
  178.  
  179. Bool      tidyBufEndOfInput( TidyBuffer* buf )
  180. {
  181.     return ( buf->next >= buf->size );
  182. }
  183.  
  184. void      tidyBufUngetByte( TidyBuffer* buf, byte bv )
  185. {
  186.     if ( buf->next > 0 )
  187.     {
  188.         --buf->next;
  189.         assert( bv == buf->bp[ buf->next ] );
  190.     }
  191. }
  192.  
  193.